﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PHTShop
{
    public partial class WebForm9 :basePageUser
    {
        List<Items> cart = new List<Items>();

        public List<Items> Cart
        {
            get { return cart; }
            set { cart = value; }
        }

        string bussiness;
        string cancel;
        string success;

        public string Success
        {
            get { return success; }
            set { success = value; }
        }

        public string Cancel
        {
            get { return cancel; }
            set { cancel = value; }
        }
        public string Bussiness
        {
            get { return bussiness; }
            set { bussiness = value; }
        }

        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
           
            List<Items> cart = (List<Items>)Session["Cart"];
            this.Cart = cart;

            Setting paypalUsername = this.dc.Settings.Single(t => t.type.Equals("Paypal Info") && t.name.Equals("Paypal Username"));
            this.Bussiness = paypalUsername.value;

            Setting urlCancel = this.dc.Settings.Single(t => t.type.Equals("Paypal Info") && t.name.Equals("UrlCancel"));
            this.Cancel = urlCancel.value;

            Setting urlSuccess = this.dc.Settings.Single(t => t.type.Equals("Paypal Info") && t.name.Equals("UrlSuccess"));
            this.Success = urlSuccess.value;

            if (cart == null)
            {
                this.LinkButtoncheckOut.Visible = false;
            }

            switch (Session["languages"].ToString().ToLower())
            {
                case "france":
                    Page.Title = "Panier";
                    break;
                case "germany":
                    Page.Title = "Warenkorb";
                    break;
                default:
                    Page.Title = "Cart";
                    break;
            }

            if (!IsPostBack)
            {
                
                    this.showProduct();
                    
                    this.loadPayment();
                    this.showProduct2();
                    this.RadioButtonListPayment.Items[0].Selected = true;
                    this.RadioButtonListPayment.SelectedIndex = 0;
                
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        private void showProduct()
        {
            List<Items> cart = (List<Items>)Session["Cart"];

            if (cart == null || cart.Count == 0)
            {
                this.LinkButtoncheckOut.Visible = false;
            }
                this.RepeaterCart.DataSource = cart;
                this.RepeaterCart.DataBind();

                this.Labeltotal.Text = this.total().ToString();
        }

        private void showProduct2()
        {
            List<Items> cart = (List<Items>)Session["Cart"];
            this.RepeaterCart2.DataSource = cart;
            this.RepeaterCart2.DataBind();

            this.Labeltotal2.Text = this.total().ToString();
        }
        
        private decimal total()
        {
            decimal s = 0;
            List<Items> cart = (List<Items>)Session["Cart"];
            if (cart != null)
            {
                foreach (Items it in cart)
                    s += it.pro.price.Value * it.qua;
            }
            return s;
        }

        protected void LinkButtondelete_click(object sender, EventArgs e)
        {
            LinkButton linkButtonDeleteItem = (LinkButton)sender;
            HiddenField hdf = (HiddenField)linkButtonDeleteItem.FindControl("HiddenField");
            int id = Convert.ToInt32(hdf.Value);
            int index = this.IndexOfItem(id);

            List<Items> cart = (List<Items>)Session["Cart"];
            cart.RemoveAt(index);
            Session["Cart"] = cart;

            this.showProduct();

            Response.Redirect("Cart.aspx");
        }
        
        private int IndexOfItem(int id)
        {
            List<Items> cart = (List<Items>)Session["Cart"];
            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].pro.id == id)
                {
                    return i;
                }
            }
            return -1;
        }

        protected void Buttonupdate_click(object sender, EventArgs e)
        {
            
            HtmlAnchor btUpdate = (HtmlAnchor)sender;
            HiddenField hdf = (HiddenField)btUpdate.FindControl("HiddenField");
            int id = Convert.ToInt32(hdf.Value);
            int index = this.IndexOfItem(id);

            TextBox TextBoxquan = (TextBox)btUpdate.FindControl("TextBoxquantity");

            List<Items> cart = (List<Items>)Session["Cart"];
            cart[index].qua = Convert.ToInt32(TextBoxquan.Text);
            Session["Cart"] = cart;

            this.showProduct();
            Response.Redirect("Cart.aspx");
        }

        private void loadPayment()
        {
            this.RadioButtonListPayment.DataSource = this.dc.PaymentMethods.Where(t => t.status == true);
            this.RadioButtonListPayment.DataTextField = "name";
            this.RadioButtonListPayment.DataValueField = "id";
            this.RadioButtonListPayment.DataBind();
        }

        private void checkAccout()
        {
            if (Session["Account"] == null)
            {
                Response.Redirect("Myaccount.aspx");
            }
        }


        protected void LinkButtoncheckOut_Click(object sender, EventArgs e)
        {
            Session["paymentId"] = this.RadioButtonListPayment.SelectedValue;
            this.checkAccout();

            this.PanelCart.Visible = false;
            this.LinkButtoncheckOut.Visible = false;
            this.LinkButtonContinue.Visible = false;

            this.PanelPay.Visible = true;
            this.yourPayment.Visible = true;
            this.LinkButtonGoPay.Visible = true;
            this.LinkButtonCancel.Visible = true;

            this.LabelYourPayment.Text = this.RadioButtonListPayment.SelectedItem.ToString();
        }

        protected void RadioButtonListPayment_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.RadioButtonListPayment.SelectedItem.Text.Equals("Paypal"))
            {
                Setting setting = this.dc.Settings.Single(t => t.type.Equals("PayPal Info") && t.name.Equals("Live"));
                if (Convert.ToInt32(setting.value) == 1)
                {
                    this.LinkButtonGoPay.PostBackUrl = "https://www.paypal.com/cgi-bin/webscr";
                }
                else
                {
                    this.LinkButtonGoPay.PostBackUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
                }

            }
            else
            {
                this.LinkButtonGoPay.PostBackUrl = "Thanks.aspx";
            }
        }

        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            this.PanelCart.Visible = true;
            this.LinkButtoncheckOut.Visible = true;
            this.LinkButtonContinue.Visible = true;

            this.PanelPay.Visible = false;
            this.yourPayment.Visible = false;
            this.LinkButtonGoPay.Visible = false;
            this.LinkButtonCancel.Visible = false;
        }
    }
}